package cn.com.haopy.dataes.db.service.service.impl;

import cn.com.haopy.dataes.db.entity.service.HCustomer;
import cn.com.haopy.dataes.db.mapper.service.HCustomerMapper;
import cn.com.haopy.dataes.db.service.service.HCustomerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Service
public class HCustomerServiceImpl extends ServiceImpl<HCustomerMapper, HCustomer> implements HCustomerService {

    public Page<HCustomer> querySelective(Integer pageNo, Integer size, String sort, String order, String keyWord) {

        QueryWrapper<HCustomer> queryWrapper = new QueryWrapper<>();

        if (Objects.nonNull(keyWord)) {
            queryWrapper.lambda().likeRight(HCustomer::getName, keyWord);
        }

        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order), sort);
        }

        Page<HCustomer> page = new Page<>(pageNo, size);

        return super.page(page, queryWrapper);
    }

    public List<HCustomer> queryLimit(Integer limit, String keyWord) {
        LambdaQueryWrapper<HCustomer> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(keyWord)) {
            queryWrapper.likeRight(HCustomer::getCode, keyWord);
        }
        queryWrapper.last("limit " + limit);
        return super.list(queryWrapper);
    }
}
